********************************************************************************
********"Macroprudential Regulation, Quantitative Easing, and Bank Lending"*****
********************************************************************************

********************************************************************************
********************************************************************************
**********************************2019******************************************
********************************************************************************
********************************************************************************
global ec1 "dT1e11 dT2e11 dT3e11 dT4e11 dT5e11 dT7e11 dT8e11 dT9e11 dT10e11 dT11e11 dT12e11"
global ec4 "dT1e14 dT2e14 dT3e14 dT4e14 dT5e14 dT7e14 dT8e14 dT9e14 dT10e14 dT11e14 dT12e14"
global regcommands ", pool(5) compact absorb(bankid time#isin) vce(cluster bankid isin)"

clear *
**#Bookmark (input)
use $inputdirectory/INB_portfolio2019, replace
gen die=0
replace die=1 if inlist(staemitit,8,9,101,94,257,67,28,29,40,86,259,92,258,105,50,55,260,276,477,479,480,574,576,577,580,587,588,591,595,799,807,812,814,816,853,910,911,913,916,921,929,940,945,950,959,974,32)&inlist(setemi,100,704,770,173,706,708,121,120)&inlist(stanziabilita,23,24)&(monthtom>=15)&(monthtom<=375)&(monthtom!=.)&time==tm(2019m8)

order bankid time cod_isin book portfolio, first
keep bankid time cod_isin book portfolio die
rename cod_isin COD_ISIN

egen dielong=max(die), by(COD_ISIN)
keep if dielong==1

keep if portfolio==2

drop portfolio
collapse (sum) book, by(bankid time COD_ISIN)

**#Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2019m8, keepus(exassets)
keep if _merge==3|_merge==2
drop _merge
replace time=tm(2019m8) if time==.
drop exassets

fillin bankid COD_ISIN time
replace book=0 if book==.
egen isin=group(COD_ISIN)
drop if isin==.
egen id=group(bankid isin)
xtset id time

**#Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2019m8, keepus(e14 e11 exlnassets excashas)
keep if _merge==3
drop _merge

**#Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_assets2019, keepus(assets)
keep if _merge!=2
drop _merge

**#Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_refinm2019, keepus(lncrefin)
keep if _merge!=2
drop _merge
*recode lncrefin (.=0)

**#Bookmark (input)
merge m:1 time COD_ISIN using $inputdirectory/INB_isinsprices2019
keep if _merge!=2
drop _merge
replace price=. if price==0
replace priceavg=. if priceavg==0

gen quantity=book/price
xtset id time

gen qsold=quantity-L.quantity
gen sales=1000000*(qsold)*priceavg/L.assets

keep if time>=tm(2019m3)&time<=tm(2020m2)
egen xxx=mean(book), by(bankid isin)
sum xxx if xxx!=0, d
drop if abs(xxx)<=10000&xxx!=0
drop xxx
winsor2 sales if sales!=0&sales!=., suffix(_w) cut(1 99)
recode sales_w (.=0)

run RO_tdummies 2019 12
run RO_tdintera 12 "e14 e11 exlnassets excashas"
run RO_labels 2019 "e14 e11"

reghdfe sales_w $ec4 $ec1 dT*exlnassets dT*excashas lncrefin $regcommands

graph drop _all
**#Bookmark (output - panel a - 1)
coefplot , keep(dT*e14) mcolor(navy) ciopts(recast(rcap) col(navy)) ytick(-2(1)2, tposition(inside) tlcolor(gs5)) ytitle("Trading{subscript:s,b,t}") ylabel(-2(1)2, notick) yline(0, lcolor(black)) vertical xlabel(,angle(90)) levels(90) saving($outputdirectory/FIG5A1,replace) title("") relocate(dT12e14=12 dT11e14=11 dT10e14=10 dT9e14=9 dT8e14=8 dT7e14=7) addplot(scatteri 0 6, mc(gs10) || pcarrowi 2 4.5 1.8 5.8 (9) "Pre Announcement Month", barbsize(1) msize(2) lw(vthin) color(gs10) mlabc(gs10) mlabs(small)) xline(6, lcolor(gs10) lpattern(dash)) text(-2.55 6 "2019m8", color(gs10) orient(vertical)) graphregion(color(white) margin(zero))

**#Bookmark (output - panel a - 2)
coefplot , keep(dT*e11) mcolor(navy) ciopts(recast(rcap) col(navy)) ytick(-1(0.5)1, tposition(inside) tlcolor(gs5)) ytitle("Trading{subscript:s,b,t}") ylabel(-1(0.5)1, notick) yline(0, lcolor(black)) vertical xlabel(,angle(90)) levels(90) saving($outputdirectory/FIG5A2,replace) title("") relocate(dT12e11=12 dT11e11=11 dT10e11=10 dT9e11=9 dT8e11=8 dT7e11=7) addplot(scatteri 0 6, mc(gs10) || pcarrowi 0.8 4.5 0.6 5.8 (9) "Pre Announcement Month", barbsize(1) msize(2) lw(vthin) color(gs10) mlabc(gs10) mlabs(small)) xline(6, lcolor(gs10) lpattern(dash)) text(-1.28 6 "2019m8", color(gs10) orient(vertical)) graphregion(color(white) margin(zero))


*==================================================================================
global ec1 "dT1e11 dT2e11 dT3e11 dT4e11 dT5e11 dT7e11 dT8e11 dT9e11 dT10e11 dT11e11 dT12e11"
global ec4 "dT1e14 dT2e14 dT3e14 dT4e14 dT5e14 dT7e14 dT8e14 dT9e14 dT10e14 dT11e14 dT12e14"
global regcommands ", pool(5) compact absorb(bankid time#isin) vce(cluster bankid isin)"

**#Bookmark (input)
clear *
use $inputdirectory/INB_portfolio2019, replace
gen die=0
replace die=1 if inlist(staemitit,8,9,101,94,257,67,28,29,40,86,259,92,258,105,50,55,260,276,477,479,480,574,576,577,580,587,588,591,595,799,807,812,814,816,853,910,911,913,916,921,929,940,945,950,959,974,32)&inlist(setemi,100,704,770,173,706,708,121,120)&inlist(stanziabilita,23,24)&(monthtom>=15)&(monthtom<=375)&(monthtom!=.)&time==tm(2019m8)

order bankid time cod_isin book portfolio, first
keep bankid time cod_isin book portfolio die
rename cod_isin COD_ISIN

egen dielong=max(die), by(COD_ISIN)
keep if dielong==1

keep if portfolio==3

drop portfolio
collapse (sum) book, by(bankid time COD_ISIN)

**#Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2019m8, keepus(exassets)
keep if _merge==3|_merge==2
drop _merge
replace time=tm(2019m8) if time==.
drop exassets

fillin bankid COD_ISIN time
replace book=0 if book==.
egen isin=group(COD_ISIN)
drop if isin==.
egen id=group(bankid isin)
xtset id time

**#Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2019m8, keepus(e14 e11 exlnassets excashas)
keep if _merge==3
drop _merge

**#Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_assets2019, keepus(assets)
keep if _merge!=2
drop _merge

**#Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_refinm2019, keepus(lncrefin)
keep if _merge!=2
drop _merge
recode lncrefin (.=0)

**#Bookmark (input)
merge m:1 time COD_ISIN using $inputdirectory/INB_isinsprices2019
keep if _merge!=2
drop _merge
replace price=. if price==0
replace priceavg=. if priceavg==0

gen quantity=book/price
xtset id time

gen qsold=quantity-L.quantity
gen sales=1000000*(qsold)*priceavg/L.assets

keep if time>=tm(2019m3)&time<=tm(2020m2)
egen xxx=mean(book), by(bankid isin)
sum xxx if xxx!=0, d
drop if abs(xxx)<=10000&xxx!=0
drop xxx
winsor2 sales if sales!=0&sales!=., suffix(_w) cut(1 99)
recode sales_w (.=0)

run RO_tdummies 2019 12
run RO_tdintera 12 "e14 e11 exlnassets excashas"
run RO_labels 2019 "e14 e11"

reghdfe sales_w $ec4 $ec1 dT*exlnassets dT*excashas lncrefin $regcommands

graph drop _all
**#Bookmark (output - panel b - 1)
coefplot , keep(dT*e14) mcolor(navy) ciopts(recast(rcap) col(navy)) ytick(-0.1(0.05)0.1, tposition(inside) tlcolor(gs5)) ytitle("Trading{subscript:s,b,t}") ylabel(-0.1(0.05)0.1, notick) yline(0, lcolor(black)) vertical xlabel(,angle(90)) levels(90) saving($outputdirectory/FIG5B1,replace) title("") relocate(dT12e14=12 dT11e14=11 dT10e14=10 dT9e14=9 dT8e14=8 dT7e14=7) addplot(scatteri 0 6, mc(gs10) || pcarrowi -0.08 4.5 -0.06 5.8 (9) "Pre Announcement Month", barbsize(1) msize(2) lw(vthin) color(gs10) mlabc(gs10) mlabs(small)) xline(6, lcolor(gs10) lpattern(dash)) text(-0.128 6 "2019m8", color(gs10) orient(vertical)) graphregion(color(white) margin(zero))

**#Bookmark (output - panel b - 2)
coefplot , keep(dT*e11) mcolor(navy) ciopts(recast(rcap) col(navy)) ytick(-0.15(0.05)0.15, tposition(inside) tlcolor(gs5)) ytitle("Trading{subscript:s,b,t}") ylabel(-0.15(0.05)0.15, notick) yline(0, lcolor(black)) vertical xlabel(,angle(90)) levels(90) saving($outputdirectory/FIG5B2,replace) title("") relocate(dT12e11=12 dT11e11=11 dT10e11=10 dT9e11=9 dT8e11=8 dT7e11=7) addplot(scatteri 0 6, mc(gs10) || pcarrowi -0.125 4.5 -0.105 5.8 (9) "Pre Announcement Month", barbsize(1) msize(2) lw(vthin) color(gs10) mlabc(gs10) mlabs(small)) xline(6, lcolor(gs10) lpattern(dash)) text(-0.19 6 "2019m8", color(gs10) orient(vertical)) graphregion(color(white) margin(zero))

********************************************************************************
********************************************************************************
**********************************2015******************************************
********************************************************************************
********************************************************************************
global ec3 "dT1e13 dT2e13 dT3e13 dT4e13 dT5e13 dT7e13 dT8e13 dT9e13 dT10e13 dT11e13 dT12e13"
global ec5 "dT1e15 dT2e15 dT3e15 dT4e15 dT5e15 dT7e15 dT8e15 dT9e15 dT10e15 dT11e15 dT12e15"
global regcommands ", pool(5) compact absorb(bankid time#isin) vce(cluster bankid isin)"

clear *
**#Bookmark (input)
use $inputdirectory/INB_portfolio2015, replace
gen die=0
replace die=1 if inlist(staemitit,8,9,101,94,257,67,28,29,40,86,259,92,258,105,50,55,260,276,477,479,480,574,576,577,580,587,588,591,595,799,807,812,814,816,853,910,911,913,916,921,929,940,945,950,959,974)&inlist(setemi,100,704,770)&inlist(stanziabilita,23,24)&(monthtom>=27)&(monthtom<=375)&(monthtom!=.)&time==tm(2014m12)

order bankid time cod_isin book portfolio, first
keep bankid time cod_isin book portfolio die
rename cod_isin COD_ISIN

egen dielong=max(die), by(COD_ISIN)
keep if dielong==1

keep if portfolio==2

drop portfolio
collapse (sum) book, by(bankid time COD_ISIN)

**#Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2014m12, keepus(exassets)
keep if _merge==3|_merge==2
drop _merge
replace time=tm(2014m12) if time==.
drop exassets

fillin bankid COD_ISIN time
replace book=0 if book==.
egen isin=group(COD_ISIN)
drop if isin==.
egen id=group(bankid isin)
xtset id time

**#Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2014m12, keepus(e13 e15 exlnassets excashas)
keep if _merge==3
drop _merge

**#Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_assets2015, keepus(assets)
keep if _merge!=2
drop _merge

**#Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_refinm2015, keepus(lncrefin)
keep if _merge!=2
drop _merge
recode lncrefin (.=0)

**#Bookmark (input)
merge m:1 time COD_ISIN using $inputdirectory/INB_isinsprices2015
keep if _merge!=2
drop _merge
replace price=. if price==0
replace priceavg=. if priceavg==0

gen quantity=book/price
xtset id time

gen qsold=quantity-L.quantity
gen sales=1000000*(qsold)*priceavg/L.assets

keep if time>=tm(2014m7)&time<=tm(2015m6)
egen xxx=mean(book), by(bankid isin)
sum xxx if xxx!=0, d
drop if abs(xxx)<=10000&xxx!=0
drop xxx
winsor2 sales if sales!=0&sales!=., suffix(_w) cut(1 99)
recode sales_w (.=0)

run RO_tdummies 2015 12
run RO_tdintera 12 "e13 e15 exlnassets excashas"
run RO_labels 2015 "e13 e15"

reghdfe sales_w $ec3 $ec5 dT*exlnassets dT*excashas lncrefin $regcommands

graph drop _all
**#Bookmark (output - panel c - 1)
coefplot , keep(dT*e13) mcolor(navy) ciopts(recast(rcap) col(navy)) ytick(-40(20)40, tposition(inside) tlcolor(gs5)) ytitle("Trading{subscript:s,b,t}") ylabel(-40(20)40, notick) yline(0, lcolor(black)) vertical xlabel(,angle(90)) levels(90) saving($outputdirectory/FIG5C1,replace) title("") relocate(dT12e13=12 dT11e13=11 dT10e13=10 dT9e13=9 dT8e13=8 dT7e13=7) addplot(scatteri 0 6, mc(gs10) || pcarrowi 40 4.5 39 5.8 (9) "Pre Announcement Month", barbsize(1) msize(2) lw(vthin) color(gs10) mlabc(gs10) mlabs(small)) xline(6, lcolor(gs10) lpattern(dash)) text(-51 6 "2014m12", color(gs10) orient(vertical)) graphregion(color(white) margin(zero))

**#Bookmark (output - panel c - 2)
coefplot , keep(dT*e15) mcolor(navy) ciopts(recast(rcap) col(navy)) ytick(-10(5)10, tposition(inside) tlcolor(gs5)) ytitle("Trading{subscript:s,b,t}") ylabel(-10(5)10, notick) yline(0, lcolor(black)) vertical xlabel(,angle(90)) levels(90) saving($outputdirectory/FIG5C2,replace) title("") relocate(dT12e15=12 dT11e15=11 dT10e15=10 dT9e15=9 dT8e15=8 dT7e15=7) addplot(scatteri 0 6, mc(gs10) || pcarrowi 8 4.5 6 5.8 (9) "Pre Announcement Month", barbsize(1) msize(2) lw(vthin) color(gs10) mlabc(gs10) mlabs(small)) xline(6, lcolor(gs10) lpattern(dash)) text(-13 6 "2014m12", color(gs10) orient(vertical)) graphregion(color(white) margin(zero))


*==================================================================================
global ec3 "dT1e13 dT2e13 dT3e13 dT4e13 dT5e13 dT7e13 dT8e13 dT9e13 dT10e13 dT11e13 dT12e13"
global ec5 "dT1e15 dT2e15 dT3e15 dT4e15 dT5e15 dT7e15 dT8e15 dT9e15 dT10e15 dT11e15 dT12e15"
global regcommands ", pool(5) compact absorb(bankid time#isin) vce(cluster bankid isin)"

clear *
**#Bookmark (input)
use $inputdirectory/INB_portfolio2015, replace
gen die=0
replace die=1 if inlist(staemitit,8,9,101,94,257,67,28,29,40,86,259,92,258,105,50,55,260,276,477,479,480,574,576,577,580,587,588,591,595,799,807,812,814,816,853,910,911,913,916,921,929,940,945,950,959,974)&inlist(setemi,100,704,770)&inlist(stanziabilita,23,24)&(monthtom>=27)&(monthtom<=375)&(monthtom!=.)&time==tm(2014m12)

order bankid time cod_isin book portfolio, first
keep bankid time cod_isin book portfolio die
rename cod_isin COD_ISIN

egen dielong=max(die), by(COD_ISIN)
keep if dielong==1

keep if portfolio==3
drop portfolio

collapse (sum) book, by(bankid time COD_ISIN)

**#Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2014m12, keepus(exassets)
keep if _merge==3|_merge==2
drop _merge
replace time=tm(2014m12) if time==.
drop exassets

fillin bankid COD_ISIN time
replace book=0 if book==.
egen isin=group(COD_ISIN)
drop if isin==.
egen id=group(bankid isin)
xtset id time

**#Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2014m12, keepus(e13 e15 exlnassets excashas)
keep if _merge==3
drop _merge

**#Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_assets2015, keepus(assets)
keep if _merge!=2
drop _merge

**#Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_refinm2015, keepus(lncrefin)
keep if _merge!=2
drop _merge
recode lncrefin (.=0)

**#Bookmark (input)
merge m:1 time COD_ISIN using $inputdirectory/INB_isinsprices2015
keep if _merge!=2
drop _merge
replace price=. if price==0
replace priceavg=. if priceavg==0

gen quantity=book/price
xtset id time

gen qsold=quantity-L.quantity
gen sales=1000000*(qsold)*priceavg/L.assets

keep if time>=tm(2014m7)&time<=tm(2015m6)
egen xxx=mean(book), by(bankid isin)
sum xxx if xxx!=0, d
drop if abs(xxx)<=10000&xxx!=0
drop xxx
winsor2 sales if sales!=0&sales!=., suffix(_w) cut(1 99)
recode sales_w (.=0)

run RO_tdummies 2015 12
run RO_tdintera 12 "e13 e15 exlnassets excashas"
run RO_labels 2015 "e13 e15"

reghdfe sales_w $ec3 $ec5 dT*exlnassets dT*excashas lncrefin $regcommands

graph drop _all
**#Bookmark (output - panel d - 1)
coefplot , keep(dT*e13) mcolor(navy) ciopts(recast(rcap) col(navy)) ytick(-1.2(0.2)1.2, tposition(inside) tlcolor(gs5)) ytitle("Trading{subscript:s,b,t}") ylabel(-1.2(0.2)1.2, notick) yline(0, lcolor(black)) vertical xlabel(,angle(90)) levels(90) saving($outputdirectory/FIG5D1,replace) title("") relocate(dT12e13=12 dT11e13=11 dT10e13=10 dT9e13=9 dT8e13=8 dT7e13=7) addplot(scatteri 0 6, mc(gs10) || pcarrowi 1.1 4.5 0.8 5.8 (9) "Pre Announcement Month", barbsize(1) msize(2) lw(vthin) color(gs10) mlabc(gs10) mlabs(small)) xline(6, lcolor(gs10) lpattern(dash)) text(-1.55 6 "2014m12", color(gs10) orient(vertical)) graphregion(color(white) margin(zero))

**#Bookmark (output - panel d - 2)
coefplot , keep(dT*e15) mcolor(navy) ciopts(recast(rcap) col(navy)) ytick(-0.2(0.1)0.2, tposition(inside) tlcolor(gs5)) ytitle("Trading{subscript:s,b,t}") ylabel(-0.2(0.1)0.2, notick) yline(0, lcolor(black)) vertical xlabel(,angle(90)) levels(90) saving($outputdirectory/FIG5D2,replace) title("") relocate(dT12e15=12 dT11e15=11 dT10e15=10 dT9e15=9 dT8e15=8 dT7e15=7) addplot(scatteri 0 6, mc(gs10) || pcarrowi 0.18 4.5 0.16 5.8 (9) "Pre Announcement Month", barbsize(1) msize(2) lw(vthin) color(gs10) mlabc(gs10) mlabs(small)) xline(6, lcolor(gs10) lpattern(dash)) text(-0.26 6 "2014m12", color(gs10) orient(vertical)) graphregion(color(white) margin(zero))
